$(document).ready(function(){
	var type=$('#type').val();
	var pId=$('#pId').val();
	genDictSelect('sexInfo','sex','1');
	genDictSelect('nationCode','nation','1');
	initForm(type,pId);
	//事件绑定
	eventBind(type,pId);
});
//初始化form表单
function initForm(type,pId){
	$('#patientForm1').validation();
	
	if(pId==''){
		
	}else{
		$.getApiClient('/patient/select/{id}').setQuery({id: pId}).get().done(function(data){
			if(data.code=='0'){
				var payload=data.payload;
				$('#patientForm1').bindData(payload);
				genDictSelect('sexInfo','sex',payload.sex);
				genDictSelect('nationCode','nation',payload.nation);
			}
		})
	}
}
//事件绑定
function eventBind(type,pId){
	$('#cancel').click(function(){
		BootstrapDialog.closeAll();
	})
	if(pId==''){
		$('#save').on('click',saveNewPatient);
	}else{
		$('#save').on('click',function(){
			updatePatient(pId);
		});
	}
}

//新增患者操作
function saveNewPatient(){
	if(!$("#patientForm1").validationForm()){
		return;
	}
	$.getApiClient('/patient/save').post($('#patientForm1').collect(), function(params){
		return $.extend(params, {
			nation:$('#nation').multipleSelect('getSelects','text')[1],
			sex: $('#sex').multipleSelect('getSelects','text')[1]
		});
	}).done(function(data){
		if(data.code == "0"){
			BootstrapDialog.closeAll();
			customTimeOutDialog('信息提示','保存成功',null,2000);
			pageQuery();
		}else{
			customAlertDialog(data.message, '错误提示', null, null);
		}
	});
}

//修改患者操作
function updatePatient(pId){
	if(!$("#patientForm1").validationForm()){
		return;
	}
	$.getApiClient('/patient/update/{id}').setQuery({
		id:pId
	}).put($('#patientForm1').collect(), function(params){
		return $.extend(params, {
			nation:$('#nation').multipleSelect('getSelects','text')[1],
			sex: $('#sex').multipleSelect('getSelects','text')[1]
		});
	}).done(function(data){
		if(data.code == "0"){
			BootstrapDialog.closeAll();
			customTimeOutDialog('信息提示','修改成功',null,2000);
			pageQuery();
		}else{
			customAlertDialog(data.message, '错误提示', null, null);
		}
	});
}